<?
$mctime=microtime(true);
include "config.php";

// librairies
if(is_dir('core/library')){
	$handle=opendir('core/library');
	while(false!==($file=readdir($handle))){
		if(strpos($file,"lib")!==false) include('core/library/'.$file);
	}
}

// classes
if(is_dir('core/classes')){
	$handle=opendir('core/classes');
	while(false!==($file=readdir($handle))){
		if(strpos($file,"class")!==false) include('core/classes/'.$file);
	}
}

/* - initialisation des variables globales */
$GLOBALS["VUE"]=array("module"=>"","page"=>"","layout"=>"","vue"=>"","includes"=>array());
$GLOBALS["DATA"]=array();
$GLOBALS["USER"]=null;
$GLOBALS["CONFIG"]=array();

/* - includes jquery */
$GLOBALS["VUE"]["includes"][]="includes/jquery-1.7.2.min.js";
$GLOBALS["VUE"]["includes"][]="includes/jquery-ui-1.8.22.custom.min.js";
$GLOBALS["VUE"]["includes"][]="includes/jquery.tooltip.min.js";
$GLOBALS["VUE"]["includes"][]="includes/jquery.bgiframe.min.js";

// connexion BDD
$sql=new sql();

//  recup parametrage BDD
$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."config");
foreach($res as $var){
	define($var->nom,$var->valeur);
}

// recup config des modules actifs
$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."module where actif=1");
foreach($res as $var){
	$GLOBALS["CONFIG"][$var->nom]=array();
	$res2=$sql->request("select * from ".CFG_MYSQL_PREFIX."module_config where module='".$var->nom."'");
	foreach($res2 as $var2){
		$GLOBALS["CONFIG"][$var->nom][$var2->nom]=$var2->valeur;
	}
}

// Identification
include "modules/".CFG_IDENT."/main.php";
ident_init();
if(isset($_POST["login"])&&isset($_POST["mdp"])) ident_login($_POST["login"],$_POST["mdp"]);
$GLOBALS["USER"]=unserialize($_SESSION["user"]);
ident_set_datas();


/* - determination module */
$uri=substr($_SERVER["REQUEST_URI"],strlen(CFG_REP_INSTALL));
$tmp=strpos($uri,"?");
if($tmp!==false) $uri=substr($uri,0,$tmp);
//debug("uri",$uri);
$tmp=explode("/",$uri);
$GLOBALS["VUE"]["module"]=$tmp[0];
if($GLOBALS["VUE"]["module"]=='') $GLOBALS["VUE"]["module"]='portail';
if(isset($tmp[1])) $GLOBALS["VUE"]["page"]=$tmp[1];

// Vérification des droits
if(!user_droit($GLOBALS["VUE"]["module"])) header("Location: ".CFG_REP_INSTALL);

$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."module where actif=1");
foreach($res as $var){
	$module=new module($var);
	
	/*- chargement librairies */
	if(is_dir('modules/'.$module->nom.'/library')){
		$handle=opendir('modules/'.$module->nom.'/library');
		while(false!==($file=readdir($handle))){
			if(strpos($file,"lib")!==false) require_once('modules/'.$module->nom.'/library/'.$file);
		}
	}

	/*- chargement classes */
	if(is_dir('modules/'.$module->nom.'/classes')){
		$handle=opendir('modules/'.$module->nom.'/classes');
		while(false!==($file=readdir($handle))){
			if(strpos($file,"class")!==false) require_once('modules/'.$module->nom.'/classes/'.$file);
		}
	}
}

// Chargement des modules "core"
$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."module where type='core' and actif=1");
foreach($res as $var){
	$mod= new module($var);
	load_module($mod->nom, $mod);
}

/*- chargement configs */
if(file_exists('modules/'.$GLOBALS["VUE"]["module"].'/config.php')) include 'modules/'.$GLOBALS["VUE"]["module"].'/config.php';
if(file_exists('themes/'.CFG_THEME.'/config_'.$GLOBALS["VUE"]["module"].'.php')) include 'themes/'.CFG_THEME.'/'.$GLOBALS["VUE"]["module"].'/config.php';


/* - chargement page */
if(($GLOBALS["VUE"]["page"]!="")&&(file_exists("modules/".$GLOBALS["VUE"]["module"]."/".$GLOBALS["VUE"]["page"].".php"))) include "modules/".$GLOBALS["VUE"]["module"]."/".$GLOBALS["VUE"]["page"].".php";
elseif(file_exists("modules/".$GLOBALS["VUE"]["module"]."/main.php")) include "modules/".$GLOBALS["VUE"]["module"]."/main.php";

/* - selection vue */
if($GLOBALS["VUE"]["page"]!=""){
	if(file_exists("themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/view_".$GLOBALS["VUE"]["page"].".phtml")) $GLOBALS["VUE"]["vue"]="themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/view_".$GLOBALS["VUE"]["page"].".phtml";
	else $GLOBALS["VUE"]["vue"]="modules/".$GLOBALS["VUE"]["module"]."/view_".$GLOBALS["VUE"]["page"].".phtml";
}else{
	if(file_exists("themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/view.phtml")) $GLOBALS["VUE"]["vue"]="themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/view.phtml";
	else $GLOBALS["VUE"]["vue"]="modules/".$GLOBALS["VUE"]["module"]."/view.phtml";
}

/* - selection layout */
$GLOBALS["VUE"]["layout"]="themes/".CFG_THEME."/layout.phtml";
if(file_exists("modules/".$GLOBALS["VUE"]["module"]."/layout.phtml")) $GLOBALS["VUE"]["layout"]="modules/".$GLOBALS["VUE"]["module"]."/layout.phtml";
if(file_exists("themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/layout.phtml")) $GLOBALS["VUE"]["layout"]="themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/layout.phtml";
if(($GLOBALS["VUE"]["page"]!="")&&(file_exists("themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/layout_".$GLOBALS["VUE"]["page"].".phtml"))) $GLOBALS["VUE"]["layout"]="themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/layout_".$GLOBALS["VUE"]["page"].".phtml";
	
/* - chargement includes */
$GLOBALS["VUE"]["includes"][]="<title>".CFG_TITRE_PAGE."</title>";
$GLOBALS["VUE"]["includes"][]="themes/".CFG_THEME."/script.js";
$GLOBALS["VUE"]["includes"][]="themes/".CFG_THEME."/style.css";
$GLOBALS["VUE"]["includes"][]="<script type='text/javascript' language='javacript'>REP_INSTALL='".CFG_REP_INSTALL."';</script>";
if(file_exists("themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/script.js")) $GLOBALS["VUE"]["includes"][]="themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/script.js";
elseif(file_exists("modules/".$GLOBALS["VUE"]["module"]."/script.js")) $GLOBALS["VUE"]["includes"][]="modules/".$GLOBALS["VUE"]["module"]."/script.js";
if(file_exists("themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/script_".$GLOBALS["VUE"]["page"].".js")) $GLOBALS["VUE"]["includes"][]="themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/script_".$GLOBALS["VUE"]["page"].".js";
elseif(file_exists("modules/".$GLOBALS["VUE"]["module"]."/script_".$GLOBALS["VUE"]["page"].".js")) $GLOBALS["VUE"]["includes"][]="modules/".$GLOBALS["VUE"]["module"]."/script_".$GLOBALS["VUE"]["page"].".js";
if(file_exists("themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/style.css")) $GLOBALS["VUE"]["includes"][]="themes/".CFG_THEME."/".$GLOBALS["VUE"]["module"]."/style.css";
elseif(file_exists("modules/".$GLOBALS["VUE"]["module"]."/style.css")) $GLOBALS["VUE"]["includes"][]="modules/".$GLOBALS["VUE"]["module"]."/style.css";

/* Durée génération de la page */
if(strpos($mctime," ")!==false){
	$tmp1=explode(" ",$mctime);
	$tmp2=explode(" ",microtime());
	$GLOBALS["DATA"]["TIMEGEN"]=round(($tmp2[0]+$tmp2[1])-($tmp1[0]+$tmp1[1]),3);
}else{
	$GLOBALS["DATA"]["TIMEGEN"]=round(microtime(true)-$mctime,3);
}

/* - affichage layout / vue */
ob_start("create_header");
if(file_exists($GLOBALS["VUE"]["layout"])&&(file_exists($GLOBALS["VUE"]["vue"]))){
	include $GLOBALS["VUE"]["layout"];
}else{
	error_vue();
}
ob_end_flush();

?>